package com.woniu.service.impl;

import com.woniu.entity.Unit;
import com.woniu.mapper.UnitMapper;
import com.woniu.service.UnitService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UnitServiceImpl implements UnitService {
    @Resource
    private UnitMapper unitMapper;
    @Override
    public ResponseUtil delete(Integer unitId) {
        int delete = unitMapper.delete(unitId);
        return delete>0?ResponseUtil.get(ResponseEnum.OK)
                :ResponseUtil.get(ResponseEnum.FAIL);    }

    @Override
    public ResponseUtil add(Unit unit) {
        Unit unit1 = unitMapper.findByName(unit.getUnitName());
        if (unit1 == null) {
            unit.setStatus(1);
            int add = unitMapper.add(unit);
            return add > 0 ? ResponseUtil.get(ResponseEnum.OK)
                    : ResponseUtil.get(ResponseEnum.FAIL);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    @Override
    public ResponseUtil findByUnitId(Integer unitId) {
        Unit unit = unitMapper.findByUnitId(unitId);
        return unit!=null?ResponseUtil.get(ResponseEnum.OK,unit)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil update(Unit unit) {
        int update = unitMapper.update(unit);
        return update>0?ResponseUtil.get(ResponseEnum.OK)
                :ResponseUtil.get(ResponseEnum.FAIL);

    }

    @Override
    public ResponseUtil findAll() {
        List<Unit> unitList = unitMapper.findAll();
        System.out.println(unitList);
        return !unitList.isEmpty()?ResponseUtil.get(ResponseEnum.OK,unitList)
                :ResponseUtil.get(ResponseEnum.FAIL);

    }

    @Override
    public ResponseUtil findByName(String unitName) {
        Unit unit = unitMapper.findByName(unitName);
        return unit!=null?ResponseUtil.get(ResponseEnum.OK,unit)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }
}
